package com.tim.app.ui.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.EventTag;
import com.tim.app.server.entry.Story;
import com.tim.app.ui.adapter.found.PlayListAdapter;

import java.util.List;


/**
 * 播放列表
 */
public class StoryListPop extends PopupWindow implements View.OnClickListener, BaseRecyclerAdapter.OnItemClickListener {

    private Context mContext;
    private WrapRecyclerView wrvPlayList;
    private PlayListAdapter adapter;
    private List<Story> storyList;
    private int currentIndex = 0;

    private TextView tvTitle;
    private ImageView ivClose;

    public StoryListPop(Context context, List<Story> storyList, int index) {
        this.mContext = context;
        this.storyList = storyList;
        this.currentIndex = index;
        setWidth(RT.getScreenWidth());
        setHeight((int) (RT.getScreenHeight() * 0.7));
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());
        View view = LayoutInflater.from(context).inflate(R.layout.popup_story_list, null);
        setContentView(view);
        initView(view);
        if (null != storyList) {
            tvTitle.setText(context.getString(R.string.playListTitle, String.valueOf(storyList.size())));
        }
    }

    private void initView(View view) {
        wrvPlayList = (WrapRecyclerView) view.findViewById(R.id.wrvPlayList);
        wrvPlayList.setOverScrollMode(View.OVER_SCROLL_NEVER);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        wrvPlayList.setLayoutManager(layoutManager);

        wrvPlayList.setHasFixedSize(true);
        wrvPlayList.setNestedScrollingEnabled(false);
        adapter = new PlayListAdapter(mContext, storyList);

        adapter.setOnItemClickListener(this);
        wrvPlayList.setAdapter(adapter);

        adapter.setCurrentIndex(currentIndex);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        ivClose = (ImageView) view.findViewById(R.id.ivClose);
        ivClose.setOnClickListener(this);
    }

    @Override
    public void onItemClick(View view, int position, long id) {
        EventManager.ins().sendEvent(EventTag.GO_INDEX, 0, 0, position);
        currentIndex = position;
        adapter.setCurrentIndex(position);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivClose:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1f);
    }

    public void show(View parent) {
        if (!isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            backgroundAlpha(0.5f);
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
}
